package com.guyj.baseapp.demo.bgazxing;

import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;

import com.guyj.baseapp.R;
import com.guyj.common.base.BaseActivity;
import com.guyj.common.utils.ImagePickUtils;
import com.guyj.common.utils.LogUtils;
import com.guyj.common.utils.QRCodeUtils;
import com.guyj.common.utils.ToastUtil;

import java.util.List;

import butterknife.Bind;
import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zxing.ZXingView;
import cn.finalteam.galleryfinal.model.PhotoInfo;

/**
 * 功能说明:
 * 使用方法：扫码相应解析的速度被阿里巴巴开源的freeline有点干扰，正式包去掉freeline即可，速度恢复正常
 */
public class BgaScanActivity extends BaseActivity implements QRCodeView.Delegate {
    private static final int REQUEST_CODE_CHOOSE_QRCODE_FROM_GALLERY = 666;

    @Bind(R.id.zxingview)
    ZXingView mQRCodeView;

    @Override
    protected void onStart() {
        super.onStart();
        mQRCodeView.startCamera();
//        mQRCodeView.startCamera(Camera.CameraInfo.CAMERA_FACING_FRONT);
    }

    @Override
    protected void onStop() {
        mQRCodeView.stopCamera();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        mQRCodeView.onDestroy();
        super.onDestroy();
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_bga_scan;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setBack();
        setTitle("bga扫描二维码or条码");
        mQRCodeView.setDelegate(this);
    }

    @Override
    protected void initVariable() {

    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {

    }

    private void vibrate() {
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(200);
    }

    @Override
    public void onScanQRCodeSuccess(String result) {
        LogUtils.d(TAG, "result:" + result);
        ToastUtil.showToast(result);
        vibrate();
        mQRCodeView.startSpot();
    }

    @Override
    public void onScanQRCodeOpenCameraError() {
        LogUtils.e(TAG, "打开相机出错");
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start_spot:
                mQRCodeView.startSpot();
                break;
            case R.id.stop_spot:
                mQRCodeView.stopSpot();
                break;
            case R.id.start_spot_showrect:
                mQRCodeView.startSpotAndShowRect();
                break;
            case R.id.stop_spot_hiddenrect:
                mQRCodeView.stopSpotAndHiddenRect();
                break;
            case R.id.show_rect:
                mQRCodeView.showScanRect();
                break;
            case R.id.hidden_rect:
                mQRCodeView.hiddenScanRect();
                break;
            case R.id.start_preview:
                mQRCodeView.startCamera();
                break;
            case R.id.stop_preview:
                mQRCodeView.stopCamera();
                break;
            case R.id.open_flashlight:
                mQRCodeView.openFlashlight();
                break;
            case R.id.close_flashlight:
                mQRCodeView.closeFlashlight();
                break;
            case R.id.scan_barcode:
                mQRCodeView.changeToScanBarcodeStyle();
                break;
            case R.id.scan_qrcode:
                mQRCodeView.changeToScanQRCodeStyle();
                break;
            case R.id.choose_qrcde_from_gallery:
                /*
                从相册选取二维码图片，这里为了方便演示，使用的是
                https://github.com/bingoogolapple/BGAPhotoPicker-Android
                这个库来从图库中选择二维码图片，这个库不是必须的，你也可以通过自己的方式从图库中选择图片
                 */
//                startActivityForResult(BGAPhotoPickerActivity.newIntent(this, null, 1, null), REQUEST_CODE_CHOOSE_QRCODE_FROM_GALLERY);
                ImagePickUtils.openImagePick(REQUEST_CODE_CHOOSE_QRCODE_FROM_GALLERY, 1, new ImagePickUtils.ImagePickListener() {
                    @Override
                    public void onHanlderSuccess(int requestCode, List<PhotoInfo> resultList) {
                        mQRCodeView.showScanRect();
                        QRCodeUtils.DecodeQRCode(resultList.get(0).getPhotoPath(), new QRCodeUtils.QRCodeDecodeListener(){
                            @Override
                            public void onDecodeSuccess(String result) {
                                ToastUtil.showToast(result+"\n扫码慢是因为freeline的关系");
                            }
                        });
                    }

                    @Override
                    public void onHanlderFailure(int requestCode, String errorMsg) {
                        mQRCodeView.showScanRect();
                    }
                });
                break;
        }
    }
}